package com.java.module.chat.controller.api;

import java.util.List;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import com.java.common.lang.annotation.Log;
import com.java.common.lang.domain.AjaxResult;
import com.java.module.chat.domain.dto.SessionDTO;
import com.java.module.chat.service.IChatGroupService;
import com.java.module.chat.domain.ChatMessage;
import com.java.module.chat.domain.Session;
import com.java.module.chat.domain.dto.ChatMessageDTO;
import com.java.module.chat.domain.vo.ChatMessageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.java.common.lang.domain.ReturnVO;
import com.java.common.lang.domain.page.Page;
import com.java.common.lang.domain.page.PageDomain;
import com.java.common.service.ApiTokenService;
import com.java.common.utils.StringUtils;
import com.java.common.utils.WebKit;
import com.java.common.utils.crypto.RsaKit;
import com.java.module.chat.service.IChatMessageService;
import com.java.module.chat.service.ISessionService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * ChatController
 * 
 * @author Administrator
 * @date 2021-09-29
 */
@ApiSort(2000)
@Api(tags = "聊天管理")
@RestController("ApiChatController")
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    IChatMessageService chatMessageService;

    @Autowired
    ISessionService sessionService;

    @Autowired
    ApiTokenService tokenService;
    @Autowired
    IChatGroupService chatGroupService;

    @ApiOperationSupport(order = 10)
    @ApiOperation("消息列表")
    @GetMapping("/getMessageList")
    public Page<ChatMessageVO> getMessageList(ChatMessage body, @Validated PageDomain page) {
        body.setMsgFromUid(tokenService.getLoginUserId());
        return WebKit.getPageData(() -> chatMessageService.selectChatMessageHistory(body));
    }

    @ApiOperationSupport(order = 20)
    @ApiOperation("发送消息")
    @PostMapping("/sendMessage")
    public ReturnVO<ChatMessageVO> sendMessage(@Validated @RequestBody ChatMessageDTO body) {
        body.setMsgFromUid(tokenService.getLoginUserId());
        return chatMessageService.sendMessage(body);
    }

    @ApiOperationSupport(order = 30)
    @ApiOperation("设置已读")
    @PostMapping("/readAllMessage")
    public int readAllMessage(@RequestBody ChatMessageDTO body) {
        body.setMsgToUid(tokenService.getLoginUserId());
        return chatMessageService.readAllMessage(body);
    }

    @ApiOperationSupport(order = 40)
    @ApiOperation("会话列表")
    @GetMapping("/getSessionList")
    public List<Session> getSessionList(ChatMessage body) {

        return sessionService.getSessionList(tokenService.getLoginUserId(),body);
    }


    @ApiOperationSupport(order = 40)
    @ApiOperation("查询官方客服会话列表")
    @GetMapping("/getAdminSession")
    public Session getAdminSession() {
        return sessionService.getAdminSession(tokenService.getLoginUserId());
    }

    @ApiOperationSupport(order = 45)
    @ApiOperation("未读数量")
    @GetMapping("/getUserUnreadCount")
    public Integer getUserUnreadCount() {
        return sessionService.getUserUnreadCount(tokenService.getLoginUserId());
    }

    @ApiOperationSupport(order = 50)
    @ApiOperation("删除会话")
    @PostMapping("/delSession/{sessionId}")
    public int delSessionList(@Validated @NotNull @PathVariable("sessionId") Long sessionId) {
        return sessionService.deleteSessionById(sessionId);
    }



    @ApiOperationSupport(order = 50)
    @ApiOperation("删除当前用户的全部消息记录")
    @PostMapping("/delChat/{uid}")
    public int delChat(@Validated @NotNull @PathVariable("uid") Long uid) {

        SessionDTO session = new SessionDTO();
        session.setUid(tokenService.getLoginUserId());
        session.setOtherSideUid(uid);
        List<Session> list = sessionService.selectSessionList(session);
        if (list != null && list.size() > 0) {
            sessionService.deleteSessionById(list.get(0).getSessionId());
        }
        return 1;
    }

    @ApiOperationSupport(order = 60)
    @ApiOperation("查询用户")
    @GetMapping("/getToUser")
    public AjaxResult getToUser(@Validated @NotBlank String token,@Validated @NotBlank  Integer type) throws Exception {
        long uid = StringUtils.isBlank(token) ? tokenService.getLoginUserId() : Long.parseLong(RsaKit.decrypt(token));
        return AjaxResult.success("data",sessionService.getGroupInfo(uid,type,tokenService.getLoginUserId()));
    }
    @ApiOperationSupport(order = 60)
    @ApiOperation("修改群聊名称")
    @GetMapping("/changeGroupName")
    public int changeGroupName(@Validated @NotBlank String token,@Validated @NotBlank String name) throws Exception {
        Long gid =  Long.parseLong(RsaKit.decrypt(token));
        Long uid=tokenService.getLoginUserId();
        return chatGroupService.changeGroupName(uid,gid,name);
    }

    @ApiOperationSupport(order = 60)
    @ApiOperation("增加会话记录")
    @GetMapping("/addSession")
    public AjaxResult addSession(@Validated @NotBlank Long otherSideUid,@Validated @NotBlank  Integer sessionType) {
        Long userId = tokenService.getLoginUserId();
        return AjaxResult.success("data",sessionService.addSession(otherSideUid,sessionType,userId,0));
    }
}